package com.aaa.ss.service.impl;

import com.aaa.common.bo.Order;
import com.aaa.ss.dao.CommentDao;
import com.aaa.ss.entity.Comment;
import com.aaa.ss.service.CommentService;
import com.aaa.ss.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2020-11-19 09:04:47
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentDao commentDao;

    @Autowired
    private OrderService orderService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Map queryById(Long id) {
        Comment comment = this.commentDao.queryById(id);
        Order order = new Order();
        order.setId(Integer.valueOf(comment.getOrderId()+""));
        List<Order> orders = orderService.listOrder(order);
        Map map = new HashMap();
        map.put("comment",comment);
        map.put("orderList",orders);
        return map;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Comment> queryAllByLimit(int offset, int limit) {
        return this.commentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment insert(Comment comment) {
        this.commentDao.insert(comment);
        return comment;
    }

    /**
     * 修改数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Override
    public Comment update(Comment comment) {
        this.commentDao.update(comment);
        return (Comment) this.queryById(comment.getId()).get("comment");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.commentDao.deleteById(id) > 0;
    }
}